<!--  -->
<template>
  <div>
    <el-table style="width: 100%" border :data="skuList">
      <el-table-column type="index" label="序号" width="100" align="center" />
      <el-table-column prop="skuName" label="名称" width="width" />
      <el-table-column prop="skuDesc" label="描述" width="width" />
      <el-table-column prop="prop" label="默认图片" width="120">
        <!-- eslint-disable-next-line -->
        <template slot-scope="{row, $index}">
          <img :src="row.skuDefaultImg" style="width:100px;height:100px" alt="">
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量" width="100" />
      <el-table-column prop="price" label="价格" width="100" />
      <el-table-column label="操作" width="width">
        <!-- eslint-disable-next-line -->
        <template slot-scope="{row, $index}">
          <el-button v-if="row.isSale == 0" type="info" icon="el-icon-top" size="mini" title="上架SKU" @click="upSale(row)" />
          <el-button v-else type="success" icon="el-icon-bottom" size="mini" title="下架SKU" @click="downSale(row)" />
          <el-button type="primary" icon="el-icon-edit" size="mini" title="编辑SKU" @click="editSku" />
          <el-button type="info" icon="el-icon-info" size="mini" title="查看SKU信息" @click="lookSkuInfo(row)" />
          <el-button type="danger" icon="el-icon-delete" size="mini" title="删除SKU" />
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="page"
      :page-sizes="[10, 20, 50]"
      :page-size="limit"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="total"
      style="text-align: center;"
      @size-change="handleSizeChange"
      @current-change="getSkuList"
    />

    <el-drawer :visible.sync="drawer" :before-close="handleClose" :show-close="false" size="50%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}元</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <template>
            <!-- eslint-disable-next-line -->
            <el-tag v-for="(attr,index) in skuInfo.skuAttrValueList" :key="attr.id" type="success" style="margin-right:10px">{{ attr.attrId }}-{{ attr.valueId }}</el-tag>
          </template>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <el-carousel height="300px">
            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
              <img :src="item.imgUrl" style="width:512px;height:300px">
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: 'Sku',
  data() {
    return {
      page: 1,
      limit: 10,
      skuList: [],
      total: 0,
      skuInfo: {},
      drawer: false
    }
  },
  mounted() {
    this.getSkuList()
  },
  methods: {
    async getSkuList(pages = 1) {
      this.page = pages
      const { page, limit } = this
      const result = await this.$API.sku.reqSkuList(page, limit)
      if (result.code === 200) {
        this.skuList = result.data.records
        this.total = result.data.total
      }
    },
    handleSizeChange(limit) {
      this.limit = limit
      this.getSkuList()
    },
    async upSale(sku) {
      const result = await this.$API.sku.reqSale(sku.id)
      if (result.code === 200) {
        // eslint-disable-next-line require-atomic-updates
        sku.isSale = 1
        this.$message({
          type: 'success',
          message: '商品上架成功'
        })
      } else {
        this.$message('操作失败，请稍后再试！')
      }
    },
    async downSale(sku) {
      const result = await this.$API.sku.reqCancel(sku.id)
      if (result.code === 200) {
        // eslint-disable-next-line require-atomic-updates
        sku.isSale = 0
        this.$message({
          type: 'info',
          message: '商品下架成功'
        })
      } else {
        this.$message('操作失败，请稍后再试！')
      }
    },
    editSku() {
      this.message({
        type: 'info',
        message: '功能开发中...'
      })
    },
    async lookSkuInfo(sku) {
      this.drawer = true
      const result = await this.$API.sku.reqSkuById(sku.id)
      if (result.code === 200) {
        this.skuInfo = result.data
      }
    },
    handleClose(done) {
      this.skuInfo = {}
      this.drawer = false
      done()
    }
  }
}
</script>

<style>
  .el-carousel__item h3 {
    color: #475669;
    font-size: 14px;
    opacity: 0.75;
    line-height: 150px;
    margin: 0;
  }

  .el-carousel__item:nth-child(2n) {
     background-color: #99a9bf;
  }

  .el-carousel__item:nth-child(2n+1) {
     background-color: #d3dce6;
  }
</style>

<style  scoped lang="css">
 .el-row .el-col-5{
    font-size:18px;
    text-align:right;
  }
  .el-col{
    margin:10px 10px;
  }
  >>> .el-carousel__button{
    width:10px;
    height:10px;
    background:pink;
    border-radius:50%;
  }
</style>
